package com.fileupload.fileupload.controller;

import com.fileupload.fileupload.domain.entity.ChunkFile;
import com.fileupload.fileupload.domain.vo.R;
import com.fileupload.fileupload.service.FileUploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/file")
public class FileUploadController {

    @Autowired
    private FileUploadService fileUploadService;
    @PostMapping("/excel/upload")
    public ResponseEntity<Object> uploadChunk(@RequestPart("file") MultipartFile file,
                                              @RequestParam("chunkIndex") int chunkIndex,
                                              @RequestParam("extension") String extension,
                                              @RequestParam("totalChunks") int totalChunks,
                                              @RequestParam("fileId") String fileId) {
        try {
            ChunkFile request = new ChunkFile(chunkIndex, totalChunks, fileId, extension, file);
            R result = fileUploadService.saveChunk(request);
            return new ResponseEntity<>(result, HttpStatus.OK);
        } catch (IOException e) {
            return new ResponseEntity<>("上传失败", HttpStatus.INTERNAL_SERVER_ERROR);
        }

    }

}
